import { createRouter, createWebHashHistory } from 'vue-router';
import Loading from '@sutpc/vue3-loading';
import routerConfig from '@/router/config';

// import type { NProgressOptions } from 'nprogress';
// import NProgress from 'nprogress';
// import 'nprogress/nprogress.css';
const appkey = import.meta.env.VITE_APP_KEY;
const routes = [
  {
    path: '/',
    redirect: `/biz/${appkey}`
  },
  {
    path: '/biz',
    component: () => import('@/layout/index.vue'),
    children: routerConfig
  },
  {
    path: '/404',
    component: () => import('@/layout/index.vue'),
    name: 'NoFind'
  },
  {
    path: '/:path(.*)*',
    redirect: '/404'
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: routes,
  scrollBehavior: () => ({ left: 0, top: 0 })
});

// NProgress.configure({ showSpinner: false } as NProgressOptions);
// setTimeout(() => {
//   const bar: any = document.getElementById('nprogress')?.getElementsByClassName('bar')[0];
//   if (bar) {
//     bar.style.background = `var(--el-color-primary)`;
//   }
// });

export const setupRouter = (app) => {
  router.beforeEach((to, from, next) => {
    // console.log('to: ', to, router);
    const list = router.getRoutes();
    // NProgress.start();

    Loading.clear();
    next();
  });

  router.afterEach((to) => {
    // NProgress.done();
  });

  app.use(router);
};

export default router;
